import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.sql.ResultSet;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import FitNess.FenetreConnexion;
import net.proteanit.sql.DbUtils;

public class FenetreClient extends JPanel implements ActionListener,
		MouseListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	JPanel pan, panel_1;
	TextField textField;
	JButton btbtnRechercher, btnAjouter, btnRechercher, btnModifier,
			btnSupprimer;
	JTable jTableClient;
	JScrollPane monJScrollPane;

	public FenetreClient(String req) {
		pan = new JPanel();
		JPanel panel = new JPanel(new BorderLayout());
		panel.setBounds(0, 26, 759, 82);
		pan.add(panel);
		panel.setLayout(null);
		panel.setBackground(new Color(0,51,51));
		pan.setBackground(Color.WHITE);
		textField = new TextField();
		textField.setBounds(10, 20, 175, 34);
		panel.add(textField);

		btnRechercher = new JButton("Rechercher");
		btnRechercher.addActionListener(this);
		btnRechercher.setBounds(191, 20, 147, 34);
		panel.add(btnRechercher);

		btnAjouter = new JButton("Ajouter");
		btnAjouter.setBounds(344, 20, 124, 34);
		btnAjouter.addActionListener(this);
		panel.add(btnAjouter);

		btnModifier = new JButton("Modifier");
		btnModifier.setBounds(477, 20, 121, 34);
		btnModifier.addActionListener(this);
		panel.add(btnModifier);

		btnSupprimer = new JButton("Supprimer");
		btnSupprimer.setBounds(604, 20, 134, 34);
		btnSupprimer.addActionListener(this);
		panel.add(btnSupprimer);

		panel_1 = new JPanel(new BorderLayout());
		panel_1.setBounds(0, 135, 823, 315);
		pan.add(panel_1);
		btnAjouter.setIcon(new ImageIcon("icone/add.png"));
		btnModifier.setIcon(new ImageIcon("icone/modifier.png"));
		btnRechercher.setIcon(new ImageIcon("icone/search.png"));
		btnSupprimer.setIcon(new ImageIcon("icone/remo.png"));
		;
		pan.setLayout(null);
		monJScrollPane = new JScrollPane();
		jTableClient = new JTable();
		monJScrollPane.setViewportView(jTableClient);
		ResultSet rs = TestConnectionBDMysql.ExecuterRequeteSelection(req);
		jTableClient.setModel(DbUtils.resultSetToTableModel(rs));
		jTableClient.addMouseListener(this);
		panel_1.add(monJScrollPane);
		panel_1.setBounds(0, 135, 759, 385);
		pan.add(panel_1);
		try {
			if (FenetreConnexion.getPrivilege().equals("moniteur")){
				btnAjouter.setEnabled(false);
				btnSupprimer.setEnabled(false);
			}
		} catch (IOException e) {
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		
		if (e.getSource() == btnRechercher) {
			String rech = textField.getText();
			String req = "select idClient as Identifiant,nomCli as Nom,prenomCli as Prenom,mailCli as Mail,numTelCli as 'Numero Telephone',adresseCli as Adresse,ageCli as 'Date De Naissance',genreCli as Genre from Client where nomCli Like '"
					+ rech + "%' or prenomCli Like '" + rech + "%'";
			gestionPanelAffiche(new FenetreClient(req).getPanel1());
		}if (e.getSource() == btnAjouter) {
			try {
				FenetreMenu.gestionPanelAffiche(new FenetreAjouterClient().getPanneau());
			} catch (IOException e1) {
			}
		}
		if (action) {
			if (e.getSource() == btnModifier) {
				FenetreMenu.gestionPanelAffiche(new FenetreModifierClient(idCli).getPanneau());
			} else if (e.getSource() == btnSupprimer) {
				Supprimer(idCli);
				idCli = 0;
				action = false;
			}
		}
	}

	public void Supprimer(int id) {
		String req = " delete from Client where idClient='" + id + "'";
		System.out.println(id);
		int n = TestConnectionBDMysql.ExecuteRequeteUpdate(req);
		if (n == 1) {
			reAffiche("select idClient as Identifiant,nomCli as Nom,prenomCli as Prenom,mailCli as Mail,numTelCli as 'Numero Telephone',adresseCli as Adresse,ageCli as 'Date De Naissance',genreCli as Genre from Client");
		} else
			JOptionPane.showMessageDialog(this, "Identifiant Inexistant");
	}

	public JPanel getPanel() {
		return pan;
	}

	public JPanel getPanel1() {
		return panel_1;
	}

	int idCli = 0;
	boolean action = false;

	@Override
	public void mouseClicked(MouseEvent e) {
		if (e.getSource() == jTableClient) {
			if (jTableClient.getSelectedRow() >= 0) {
				idCli = (int) jTableClient.getValueAt(
						jTableClient.getSelectedRow(), 0);
				action = true;
			}
		}
	}

	public void reAffiche(String req) {

		ResultSet rs = TestConnectionBDMysql.ExecuterRequeteSelection(req);
		jTableClient.setModel(DbUtils.resultSetToTableModel(rs));
	}

	@Override
	public void mouseEntered(MouseEvent e) {
	}

	@Override
	public void mouseExited(MouseEvent e) {
	}

	@Override
	public void mousePressed(MouseEvent e) {
	}

	@Override
	public void mouseReleased(MouseEvent e) {
	}

	public void gestionPanelAffiche(JPanel panel) {
		panel_1.removeAll();
		panel_1.add(panel);
		panel_1.setVisible(true);
		panel_1.validate();
		panel_1.repaint();
	}

}
